package graphs

import (
	"power/core/dao/graphdao"
	"power/core/dao/table"
	"power/core/localwrap"
	"power/core/service/graph"

	"github.com/RocksonZeta/wrap/redised"
)

type GraphElements struct{}

func (s GraphElements) Query(param graphdao.GraphElementsQueryParam) ([]table.GraphElement, int64) {
	ds := localwrap.GetRedisedMysql()
	defer ds.Close()
	return graphdao.GraphElements{RedisedMysql: ds}.Query(param)
}
func (s GraphElements) Get(id int) graph.GraphElement {
	ds := localwrap.GetRedisedMysql()
	defer ds.Close()
	r := graphdao.GraphElements{RedisedMysql: ds}.Get_(id)
	proto := graphdao.GraphElementProtos{RedisedMysql: ds}.Get_(r.ProtoId)
	r.Props.Extend(proto.Props)
	return r
}
func (s GraphElements) ListByGraphId(graphId int) []graph.GraphElement {
	ds := localwrap.GetRedisedMysql()
	defer ds.Close()
	return s.ListByGraphId_(ds, graphId)
}
func (s GraphElements) ListByGraphId_(ds *redised.RedisedMysql, graphId int) []graph.GraphElement {
	r := graphdao.GraphElements{RedisedMysql: ds}.ListByGraphId_(graphId)
	g := graphdao.Graphs{RedisedMysql: ds}.Get(graphId)
	// protos := graphdao.GraphElementProtos{RedisedMysql: ds}.ListAsMap_(cutil.ColInt(r, "GraphId"))
	protoList := graphdao.GraphElementProtos{RedisedMysql: ds}.ListByUid(g.Creator)
	protos := graphdao.GraphElementProtos{}.List2Map(protoList)
	for i := range r {
		r[i].Props.Extend(protos[r[i].ProtoId].Props)
	}
	return r
}
func (s GraphElements) Add(param graphdao.GraphElementsAddParam) table.GraphElement {
	ds := localwrap.GetRedisedMysql()
	defer ds.Close()
	return graphdao.GraphElements{RedisedMysql: ds}.Add(param)
}
func (s GraphElements) Delete(id int) {
	ds := localwrap.GetRedisedMysql()
	defer ds.Close()
	graphdao.GraphElements{RedisedMysql: ds}.Delete(id)
}
func (s GraphElements) Deletes(ids []int) {
	ds := localwrap.GetRedisedMysql()
	defer ds.Close()
	graphdao.GraphElements{RedisedMysql: ds}.Deletes(ids)
}
func (s GraphElements) Update(param graphdao.GraphElementsUpdateParam) {
	ds := localwrap.GetRedisedMysql()
	defer ds.Close()
	graphdao.GraphElements{RedisedMysql: ds}.Update(param)
}
func (s GraphElements) UpdateProps(id int, props graph.Props) {
	ds := localwrap.GetRedisedMysql()
	defer ds.Close()
	d := graphdao.GraphElements{RedisedMysql: ds}
	old := d.Get_(id)
	props.Extend(old.Props)
	d.UpdateProps(id, props.Marshal())
}
